package com.ty.day3.collection;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 *
 * @author : txy
 * @create : 2023/11/24 16:11
 */
public class ArrayListDemo {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        //添加元素
        list.add("a");
        list.add("x");
        list.add("d");
        list.add("c");
        //元素个数
        System.out.println(list.size());
        //删除元素
        list.remove("a");
        System.out.println(list.size());
        //元素清空
        list.clear();
        System.out.println(list.size());
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("a");
        list1.add("x");
        //添加一个集合
        list.addAll(list1);
        list.add("xc");
        System.out.println(list.size());
        //清除一个集合
        list.removeAll(list1);
        System.out.println(list.size());
        //迭代器，遍历元素
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
            iterator.remove();//迭代器删除，原来的集合内容也删除
        }
        System.out.println(list.isEmpty());
    }
}
